/*
 * SM_hub.c
 *
 *  Created on: 2023年8月21日
 *      Author: zlw
 */
#include "device_type.h"
#ifdef SM_HUB
#include "app_api.h"
#include "bsp_iadc.h"
#include "bsp_gpio.h"
#include "app_log.h"
#include "softwareMultiTimer.h"
#include "RBFVersion.h"
#include "git_version.h"
#include "bsp_wdog.h"
#include "bsp_rmu.h"
#include "device.h"
#include "bsp_system.h"

static void detect_voltage_temp_start(void);
/**********************print informatio**************************/
static void printSoftWareInformation(void)
{
  char reason[12] = {0};
  APP_PRINTF("\r\n*************information***************\r\n");
  uint32_t rst_cause = ds_rmu_init();
  resetToString(rst_cause, reason);
  APP_PRINTF("reset reason   : %s\r\n",reason);
  APP_PRINTF("stack version  : v%d.%d.%d \r\n", (STACK_VERSION>>16&0xFF), (STACK_VERSION>>8&0xFF),STACK_VERSION&0xFF);
  APP_PRINTF("git  version   : 0x%x\r\n", GIT_VERSION);
  APP_PRINTF("build app time : %s %s \r\n", __DATE__, __TIME__);
#ifdef  CONFIG_JENKINS_BUILD_NUMBER
  APP_PRINTF("jenkins build number : %d\r\n", CONFIG_JENKINS_BUILD_NUMBER);
#else
  APP_PRINTF("jenkins build number : 0\r\n");//没定义默认0
#endif
  APP_PRINTF("***************************************\r\n\r\n");
}

void SM_main(void)
{
  detect_voltage_temp_start();
  printSoftWareInformation();
  startWatchDogTimer();
}

/**
 * @brief: 遥控器项目while函数相关处理
 * */
void SM_process(void)
{
  feedWatchDogHandler();
}

/*********************detect temperature ctrl************************/

static ds_gpio_t temperatureCtrl[2] = {
    {
        .port = gpioPortD,
        .pin = 3,
        .mode = gpioModePushPull,
        .initial = 0,
        .valid = 1,
    },
    {
        .port = gpioPortD,
        .pin = 2,
        .mode = gpioModeInput,
        .initial = 0,
        .valid = 1,
    }
};


static void detect_temp_ctrl_init(void)
{
  ds_gpio_init(&temperatureCtrl[0]);
}

static void detect_temp_ctrl_on(void)
{
  ds_gpio_on(&temperatureCtrl[0]);
}

void detect_temp_ctrl_off(void)
{
  ds_gpio_off(&temperatureCtrl[0]);
}

static void detect_voltage_temp_start(void)
{
  detect_temp_ctrl_init();
  ds_gpio_init(&temperatureCtrl[1]);
  detect_temp_ctrl_on();
  initIADC(iadcPosInputPortAPin8, iadcPosInputPortDPin2);
}

uint8_t get_device_state(uint8_t *buffer)
{
  (void)buffer;
  iadc_scan_start();
  uint32_t voltage = (uint32_t) (get_hub_voltage() * 90 / 22 / 10); //*100

  uint32_t temp = get_hub_temperature();
  uint32_t avdd = get_avdd_vlotage();
  avdd = (avdd == 0)?3300:avdd;

  uint32_t resistance = (uint32_t)(temp * 69 * 100 / avdd);  //*100
  DS_app_log_info("voltage = %ld   resistance = %ld \r\n", voltage, resistance);

  buffer[0] = voltage / 100;
  buffer[1] = voltage % 100;
  buffer[2] = resistance / 100;
  buffer[3] = resistance % 100;
  DS_app_log_info("voltage %d.%d    resistance %d.%d\r\n", buffer[0], buffer[1], buffer[2], buffer[3]);

  return 4;
}

/***************************************************************/
void SM_led_start(uint8_t *data, blinkType_t type)
{
  (void)data;
  (void)type;
}

void staticPowerGpioInit(void){}

uint8_t SM_battery_get(void)
{
  return 0;
}

int8_t SM_tempDrv_get(void)
{
  return 0;
}

/**********************get hard information***************************/

/**
 * @brief: get hard information
 * @param: point
 * @return: value length
 * */
uint8_t get_hard_information(uint8_t *data)
{
  hardinfo_t hardInfo = {0x00};
  char psn[16] = "DJ0080N00001AA";
  int ret = 0;

  ret = getThisParam(ENpbyKeyM, hardInfo.psn);
  if(ret != 0) {
      memcpy(hardInfo.psn, psn, sizeof(psn));
      setThisParam(ENpbyKeyM, hardInfo.psn);
  }

  ret = getThisParam(ENbyDeviceType, &hardInfo.deviceType);
  if(ret != 0) {
      hardInfo.deviceType = 0x00;
      setThisParam(ENbyDeviceType, &hardInfo.deviceType);
  }

  ret = getThisParam(ENbyModelType, &hardInfo.modelType);
  if(ret != 0) {
      hardInfo.modelType = 0x01;
      setThisParam(ENbyModelType, &hardInfo.modelType);
  }

  ret = getThisParam(ENbyHardType, &hardInfo.hardType);
  if(ret != 0) {
      hardInfo.hardType = 0x01;
      setThisParam(ENbyHardType, &hardInfo.hardType);
  }

  ret = getThisParam(ENpbyMacID, hardInfo.pmacID);
  if(ret != 0) {
      getMacAddr(hardInfo.pmacID);
      setThisParam(ENpbyMacID, hardInfo.pmacID);
  }



  memcpy(data, &hardInfo, sizeof(hardInfo));

  return sizeof(hardInfo);
}
#endif

